package com.kuang.demo.controller.wx;

import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bean.wx.*;
import com.kuang.demo.mapper.acg.UserMapper;
import com.kuang.demo.service.CouponService;
import com.kuang.demo.service.wx.WXCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wx/coupon")
public class WXCouponController {
    @Autowired
    WXCouponService wxcouponService;
    @Autowired
    CouponService couponService;
    @Autowired
    UserMapper userMapper;

    @GetMapping("mylist")
    public BaseRespVo couponMylist(int status, int page, int size) {
        int id = getId();
        WXCoupon couponList = wxcouponService.queryCouponList(status, page, size, id);
        return BaseRespVo.ok(couponList);
    }

    private int getId() {
        Subject subject = SecurityUtils.getSubject();
        String userName = (String) subject.getPrincipal();
        return userMapper.selectUserId(userName);
    }

    @GetMapping("list")
    public BaseRespVo couponAllList(int page, int size) {
        WXCoupon couponList = wxcouponService.queryCouponAllList(page, size);
        return BaseRespVo.ok(couponList);
    }


    @PostMapping("receive")
    public BaseRespVo couponReceive(@RequestBody CouponReceiveBo couponId) {
        Integer integer = Integer.valueOf(couponId.getCouponId());
        wxcouponService.receiveCoupon(integer);
        return BaseRespVo.ok("领取成功");
    }

    @GetMapping("selectlist")
    public BaseRespVo selectlist(int cartId) {
        CouponSelectlist couponSelectlist = wxcouponService.selectlist(cartId);
        return BaseRespVo.ok(couponSelectlist);
    }

    @PostMapping("exchange")
    public BaseRespVo couponExchange(@RequestBody CouponExechangeCode couponExchange) {
        String code = couponExchange.getCode();
        int i = wxcouponService.couponExchange(code);
        if (i == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail(740, "优惠券已兑换");
    }

}
